#include "includes.h"

extern struct msg_prepare_ok prep_ok_list;
extern int host_count;	
extern int last_attempted;
extern int last_installed;
extern int my_id;
extern int local_aru;
	
void shift_to_prepare_phase() {

	DEBUGPRINT("Shift To Prepare Phase\n",NULL);
	last_installed = last_attempted;
	struct msg_prepare *prep = create_prepare(my_id, last_installed, local_aru);
	update_data_structures(serialize_prepare(prep));
	//DEBUGPRINT("CREATING PREP_OK WITH %d\n", last_installed);
	struct msg_prepare_ok *prepok = create_prepare_ok(my_id, last_installed);
	add_prep_ok(prepok);
	clear_last_enqueued();
	sync();
	send_to_all(serialize_prepare(prep));
}

int view_prepared_ready(int view){

	//Check whether we have floor(N/2) + 1 entries
	struct msg_prepare_ok *tmp;
	struct list_head *pos;
	int count = 0;
	list_for_each(pos, &prep_ok_list.list) {
		tmp = list_entry(pos, struct msg_prepare_ok, list);
		if(tmp->view == view) {
			count ++;
			if(count >= (host_count/2 + 1)) {
				return 1;
			}
		}	
	}
	
	return 0;
}
